package com.sks.service.house.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sks.bean.pojo.Message;
import com.sks.dao.house.MessageDao;
import com.sks.service.base.BaseService;
import com.sks.service.house.MessageService;

@Service
@Transactional
public class MessageServiceBean extends BaseService<Message> implements MessageService {

	@Resource(name = "messageDao")
	public void setMessageDao(MessageDao messageDao) {
		this.setDao(messageDao);
	}
	
	@Override
	public Long countByIpAndDate(String ip, Date date) {
		StringBuffer jpql = new StringBuffer("select count(m.messageId) from Message m where 1 = 1 ");
		List params = new ArrayList();
		if (null != ip) {
			jpql.append("and m.ipAddress = ?");
			params.add(ip);
		}
		if (null != date) {
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.set(Calendar.HOUR_OF_DAY, 0);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			jpql.append("and m.createTime >= ?");
			params.add(c.getTime());
			
			c.set(Calendar.HOUR_OF_DAY, 23);
			c.set(Calendar.MINUTE, 59);
			c.set(Calendar.SECOND, 59);
			jpql.append("and m.createTime <= ?");
			params.add(c.getTime());
		}
		return (Long) this.getObjectByJpql(jpql.toString(), params.toArray());
	}
}
